package com.kdd.campus.sh.service.impl;

import com.alibaba.fastjson.JSON;
import com.kdd.campus.sh.repository.ESProductRepository;
import com.kdd.campus.sh.dto.ESProductInfo;
import com.kdd.campus.sh.po.ShProduct;
import com.kdd.campus.sh.po.User;
import com.kdd.campus.sh.service.ESManagerService;
import com.kdd.campus.sh.service.ShProductService;
import com.kdd.campus.sh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: fcp
 * @Date: 2020/3/26 20:21
 * @Description:
 */
@Service
public class ESManagerServiceImpl implements ESManagerService {
    @Autowired
    private ShProductService productService;

    @Autowired
    private UserService userService;

    @Autowired
    private ESProductRepository esProductMapper;

    @Override
    public void importProductData() {
        List<ShProduct> products = productService.findAll();
        List<ESProductInfo> esProductInfos = new ArrayList<>();
        for (ShProduct product : products) {
            User user = userService.findById(product.getUserId());
            ESProductInfo esProductInfo = ESProductInfo.builder()
                    .pid(product.getId())
                    .uid(user.getId())
                    .productImg(product.getPrimaryPicUrl())
                    .productName(product.getName())
                    .price(product.getPrice())
                    .isNew(product.getIsNew() == 1 ? "全新" : "")
                    .isHomeDelivery(product.getIsHomeDelivery() == 1 ? "包邮" : "")
                    .username(user.getHeadPortrait())
                    .username(user.getUsername()).build();
            esProductInfos.add(esProductInfo);
        }
        esProductMapper.saveAll(esProductInfos);
    }
}
